/*
 * common.h
 *
 *  Created on: Mar 15, 2012
 *      Author: yexijiang
 */

#ifndef COMMON_H_
#define COMMON_H_


#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include "string.h"


namespace event {

const unsigned long int CRAWLER_SLEEP_TIME_NANOSEC = 500000000;	//	500 millisecond
const unsigned long int CRAWLER_SAMPLE_TIME_NANOSEC = 500000000;	//	500 millisecond

inline std::string TimeToString(const time_t &timestamp)
{
	struct tm *time = gmtime(&timestamp);
	char out_buf[128];
	strftime(out_buf, 128, "%Y-%m-%d %Z %X", time);
	std::string str = out_buf;
	return str;
}

int GetCPUCount();

inline void ThreadSleep(long int sec, long int nanosec)
{
	struct timespec sleep_interval;
	sleep_interval.tv_sec = sec;
	sleep_interval.tv_nsec = nanosec;
	struct timespec remain_sleep_interval;
	nanosleep(&sleep_interval, &remain_sleep_interval);
}

inline void StringToFloat(const std::string &str, float &value)
{
	sscanf(str.c_str(), "%f", &value);
}

/*
 * Split the string by specified delim.
 */
void Split(const std::string &s, char c, std::vector<std::string> & v);

inline double BytesToKB(double bytes)
{
	return bytes / 1024;
}

inline double BytesToMB(double bytes)
{
	return bytes / 1048576;
}



};


#endif /* COMMON_H_ */
